﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LMCmdLetLib.LabManagerSoapApi;
using System.Net;
using System.Security.Cryptography;

namespace LMCmdLetLib
{
    // Service Wrapper for the Internal SOAP API 
    public class LabManager
    {
        #region privates
        AuthenticationHeader m_auth;
        LabManagerSOAPinterfaceSoapClient m_client;
        TimeSpan m_timeout;

        private String buildLmApiUrl(String lmUrl)
        {
            return lmUrl.EndsWith("/") ? String.Format("{0}SOAP/LabManager.asmx", lmUrl) : String.Format("{0}/SOAP/LabManager.asmx", lmUrl);
        }
        #endregion

        public LabManager(String lmUrl, String lmUsername, String lmPassword)
        {

            lmUrl = buildLmApiUrl(lmUrl);
            m_timeout = new TimeSpan(0, 10, 0); // 10 minutes
            System.ServiceModel.BasicHttpBinding m_basichttp = new System.ServiceModel.BasicHttpBinding();
            System.ServiceModel.EndpointAddress m_endpoint = new System.ServiceModel.EndpointAddress(lmUrl);

            m_basichttp.AllowCookies = false;
            m_basichttp.MessageEncoding = System.ServiceModel.WSMessageEncoding.Text;
            m_basichttp.UseDefaultWebProxy = true;
            m_basichttp.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport;
            m_basichttp.Security.Transport.ClientCredentialType = System.ServiceModel.HttpClientCredentialType.None;
            m_basichttp.Security.Transport.ProxyCredentialType = System.ServiceModel.HttpProxyCredentialType.None;
            m_basichttp.Security.Message.ClientCredentialType = System.ServiceModel.BasicHttpMessageCredentialType.UserName;
            m_basichttp.CloseTimeout = m_timeout;
            m_basichttp.OpenTimeout = m_timeout;
            m_basichttp.ReceiveTimeout = m_timeout;
            m_basichttp.SendTimeout = m_timeout;
            m_basichttp.MaxReceivedMessageSize = 524288;

            m_client = new LabManagerSOAPinterfaceSoapClient(m_basichttp, m_endpoint);
            m_auth = new AuthenticationHeader();

            m_auth.username = lmUsername;
            m_auth.password = lmPassword;
         
        }

        //TODO: get/set for privates?

        public AuthenticationHeader AuthHeader
        {
            get { return m_auth; }
        }

        public LabManagerSOAPinterfaceSoapClient SoapClient
        {
            get { return m_client; }
        }

        // public Organization[] GetOrganizations()  { return m_client.GetOrganizations(m_auth); }


    } // end class

    //public class CertificateAccepter : ICertificatePolicy
    //{
    //    CertificateAccepter() { }
    //    public bool CheckValidationResult(ServicePoint servicePoint, System.Security.Cryptography.X509Certificates.X509Certificate cert, WebRequest webRequest, int iProblem)
    //    {
    //        return true;
    //    }

    //} //end class
}
